package com.qianya.system.manage.web.login.controller;

import com.qianya.model.common.JWTUtil;
import com.qianya.model.pojo.login.qo.*;
import com.qianya.system.manage.util.password.PasswordUtil;
import com.qianya.system.manage.web.login.model.LoginUsers;
import com.qianya.system.manage.web.login.service.ILoginUsersService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ScopeMetadata;
import org.springframework.web.bind.annotation.*;
import com.qianya.model.common.CommonResult;
import com.qianya.model.pojo.login.vo.LoginUsersVO;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.ws.rs.GET;
import java.util.Date;

/**
 * <p>
 * 用户 前端控制器
 * </p>
 *
 * @author wy
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/login-users")
@Api(value = "LoginUsersController", tags = "用户接口")
@Slf4j
public class LoginUsersController {

    private Logger logger = LoggerFactory.getLogger(LoginUsersController.class);

    @Autowired
    private ILoginUsersService loginUsersService;
    @Resource
    HttpServletRequest request;

    @GetMapping("/find")
    @ApiOperation(value = "查找所有用户")
    public CommonResult<IPage<LoginUsersVO>> find(@Valid LoginUsersQO loginUsersQO) {

        return loginUsersService.find(loginUsersQO);

    }



    @PostMapping("/sava")
    @ApiOperation(value = "添加用户")
    public CommonResult sava(@Valid @RequestBody LoginUsersInsertQO loginUsersInsertQO) {

        return loginUsersService.create(loginUsersInsertQO);


    }



    @PutMapping("/update")
    @ApiOperation(value = "修改用户")

    public CommonResult update(@Valid @RequestBody LoginUsersupdateQO loginUsersupdateQO) {

            return loginUsersService.renewal(loginUsersupdateQO);
    }


    @DeleteMapping("/deleteById")
    @ApiOperation(value = "逻辑删除用户")
    public CommonResult deleteById(@ApiParam(value = "用户id",required = true)
                                   @NotBlank(message = "用户编号不能为空")
                                   @RequestParam(defaultValue = "",name = "uId")String uId){
        return loginUsersService.deleteById(uId);
    }


    @GetMapping("/findOrdinaryUser")
    @ApiOperation(value = "查找普通用户")
    public CommonResult findOrdinaryUser(@Valid LoginUsersQO loginUsersQO) {


        return loginUsersService.findOrdinaryUser(loginUsersQO);

    }

    @DeleteMapping("/frozenById")
    @ApiOperation(value = "冻结/解冻用户")
    public CommonResult<LoginUsersVO> frozenById(@ApiParam(value = "用户id",required = true)
                                   @NotBlank(message = "用户编号不能为空")
                                   @RequestParam(defaultValue = "",name = "uId")String uId){
        return loginUsersService.frozenById(uId);
    }


    @ApiOperation(value = "根据用户id信息查询")
    @GetMapping(value = "/view")
    public CommonResult<LoginUsersVO> view(@ApiParam(value = "用户id",required = true)
                             @NotBlank(message = "用户编号不能为空")
                             @RequestParam(defaultValue = "",name = "uId")String uId){
        return CommonResult.success(loginUsersService.findById(uId));
    }


    @PutMapping("/updateUser")
    @ApiOperation(value = "修改当前用户的信息")

    public CommonResult updateUserByid(@Valid @RequestBody LoginUsersupdateByIdQO loginUsersupdateByIdQO) {

        return loginUsersService.updateUserByid(loginUsersupdateByIdQO);
    }

    @PutMapping("/seleteUser")
    @ApiOperation(value = "查询当前用户的信息")
    public CommonResult<LoginUsersVO> seleteUser() {
        String userId = JWTUtil.getUserId(request.getHeader("token"));
        return CommonResult.success(loginUsersService.findById(userId));
    }
}
